#include "UserHandler.h"
#include <QTime>
#include "../../httpUtil/HttpUtil.h"
#include "../../entity/User.h"
#include "UserService.h"
#include "../../enum/StatusCode.h"
#include "../log/LogService.h"
#include <QDebug>

void UserHandler::registerRoutes() {
    // 登录
    HttpUtil::addRoute(HttpUtil::Method::POST, "/api/user/login",
                       [](const QString &, const QMap<QString, QString> &query, const HttpUtil::json &body) {
                           auto user = User::fromJson(body);
                           StatusCode result = UserService::getInstance().login(user);
                           return HttpUtil::json{
                               {"status", result},
                               {"msg", StatusCodeToMsg(result)}
                           };
                       });

    // 注册
    HttpUtil::addRoute(HttpUtil::Method::POST, "/api/user/register",
                       [](const QString &, const QMap<QString, QString> &query, const HttpUtil::json &body) {
                           auto user = User::fromJson(body);
                           StatusCode result = UserService::getInstance().registerUser(user);
                           return HttpUtil::json{
                               {"status", result},
                               {"msg", StatusCodeToMsg(result)}
                           };
                       });
}
